package com.madixin.chat.dto;

import com.madixin.chat.exception.ChatBaseException;
import com.madixin.chat.exception.ErrorCode;

public class ResponseResult<T> {
    private final boolean success;
    private final String message;
    private final String errCode;
    private final T result;

    private ResponseResult(boolean success, String message, String errCode, T result) {
        this.success = success;
        this.message = message;
        this.errCode = errCode;
        this.result = result;
    }

    public static <E> ResponseResult<E> success(E results) {
        return new ResponseResult<>(true, "", "", results);
    }

    public static <E> ResponseResult<E> fail(String message) {
        return new ResponseResult<>(false, message, ErrorCode.UNKNOWN.toString(), null);
    }

    public static <E> ResponseResult<E> fail(String message, ErrorCode code) {
        return new ResponseResult<>(false, message, code.toString(), null);
    }

    public static <E> ResponseResult<E> fail(ErrorCode code) {
        return new ResponseResult<>(false, code.getDescription(), code.toString(), null);
    }

    public static ResponseResult<String> fail(ChatBaseException e) {
        return new ResponseResult<>(false, e.getErrorCode().getDescription(), e.getErrorCode().toString(), null);
    }

    public boolean isSuccess() {
        return success;
    }

    public String getMessage() {
        return message;
    }

    public T getResult() {
        return result;
    }

    public String getErrCode() {
        return errCode;
    }
}
